import pandas as pd
import json

def race_to_instruction(input_path, output_path):
    # 读取Parquet文件（参考）
    df = pd.read_parquet(input_path)
    
    output_data = []
    for _, row in df.iterrows():
        # 构建instruction字段
        instruction = "Read the following passage and questions, then choose the right answer from options, the answer should be one of A, B, C, D."
        
        # 构建input字段（参考[HIT-REF]{"title": "FreedomIntelligence/sharegpt-chinese \u00b7 Datasets at Hugging Face", "snippet": "... \u6578\u64da\u70baJSON\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u5728\u672c\u5730\u6a5f\u5668\u4e0a\u6216Web\u670d\u52d9\u5668\u4e0a\u904b\u884c\u6b64\u4ee3\u78bc\uff0c\u7136\u5f8c\u4f7f\u7528Web\u700f\u89bd\u5668\u6216\u985e\u4f3c`curl`\u6216`httpie`\u7684\u5de5\u5177\u8a2a\u554f\u7aef\u9ede\uff1a\\n```bash\\n$ http GET http://localhost ...", "url": "https://huggingface.co/datasets/FreedomIntelligence/sharegpt-chinese", "position": 13}的JSON结构设计）
        input_content = f"Question: {row['question']}\n\nOptions:\n" + \
                        "\n".join([f"{chr(65+i)}. {opt}" for i, opt in enumerate(row['options'])])
        
        # 构建output字段（参考[HIT-REF]{"title": "\u89e3\u5bc6parquet \u6587\u4ef6\uff0c\u4ee5\u53ca\u5982\u4f55\u7528Python \u53bb\u5904\u7406\u5b83- \u53e4\u660e\u5730\u76c6 - \u535a\u5ba2\u56ed", "snippet": "\u5f53\u8c03\u7528\u4e00\u4e2a\u5916\u90e8\u63a5\u53e3\u65f6\uff0c\u8fd4\u56de\u7684\u6570\u636e\u683c\u5f0f\u51e0\u4e4e\u90fd\u662fJSON\u3002 ... \u6ce8\uff1a\u5c06engine \u6307\u5b9a\u4e3apyarrow\uff0c\u8868\u793a\u7528pyarrow \u53bb\u8bfb\u53d6CSV \u6587\u4ef6\uff0c\u4f46\u6570\u636e\u683c\u5f0f\u4ecd\u7136\u4f7f\u7528Numpy Array\u3002", "url": "https://www.cnblogs.com/traditional/p/17318820.html", "position": 3}的答案格式要求）
        output = row['answer'].upper()
        
        output_data.append({
            "instruction": instruction,
            "input": input_content,
            "output": output
        })
    
    # 保存为JSON文件（参考[HIT-REF]{"title": "python\u8bfb\u5199json\uff0cjson\uff0cparquet\u6587\u4ef6,\u5404\u79cd\u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u4ee3\u7801\u539f\u521b", "snippet": "\u8fd9\u4e2a\u5e93\u4e13\u95e8\u7528\u4e8e\u5c06JSON\u6570\u636e\u8f6c\u6362\u4e3aParquet\u6587\u4ef6\u683c\u5f0f\uff0c\u8fd9\u662f\u4e00\u79cd\u9ad8\u6548\u3001\u5217\u5f0f\u5b58\u50a8\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5e38\u7528\u4e8e\u5927\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4e86\u89e3JSON ...", "url": "https://blog.csdn.net/a1920993165/article/details/141243497", "position": 2}[HIT-REF]{"title": "python\u8bfb\u5199json\uff0cjson\uff0cparquet\u6587\u4ef6 - 51CTO\u535a\u5ba2", "snippet": "\u76d8\u70b9Python\u4e2d4\u79cd\u8bfb\u53d6JSON\u6587\u4ef6\u548c\u63d0\u53d6JSON\u6587\u4ef6 ... # Python\u5c06JSON\u5217\u8868\u8f6c\u6362\u4e3aParquet\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4ece\u4e00\u4e2a\u6570\u636e\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u636e\u683c\u5f0f\u3002", "url": "https://blog.51cto.com/u_15718612/11778412", "position": 4}的格式转换）
    with open(output_path, 'w', encoding='utf-8') as f:
        json.dump(output_data, f, ensure_ascii=False, indent=2)

# 使用示例
race_to_instruction("validation-00000-of-00001.parquet", "instruction_data.json")